package com.example.myapplication10_1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(1,1,1,"改成红色");
        menu.add(1,2,2,"改成蓝色");
        menu.add(1,3,3,"改成黄色");
        menu.add(1,4,4,"改成紫色");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        TextView txt = findViewById(R.id.txt1);
        switch(item.getItemId()){
            case 1:
                txt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        txt.setText("变成红色！");
                    }
                });
                txt.setTextColor(Color.RED);
                break;
            case 2:
                txt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        txt.setText("变成蓝色！");
                    }
                });
                txt.setTextColor(Color.BLUE);
                break;
            case 3:
                txt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        txt.setText("变成黄色！");
                    }
                });
                txt.setTextColor(Color.YELLOW);
                break;
            case 4:
                txt.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        txt.setText("变成紫色！");
                    }
                });
                txt.setTextColor(Color.parseColor("#800080"));
                break;

            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }
}